package ld.web.servlet;

import ld.pojo.Product;
import ld.pojo.cart;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Liudao
 * @Date: 2022-08-02-15:35
 * @Description:
 */
@WebServlet("/cart")
public class cartServlet extends HttpServlet {
    @Override
    public void init() throws ServletException {
    }

    //根据请求参数页面跳转
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //得到请求参数
        String cmd = req.getParameter("cmd");
        //根据参数跳转
        if (StringUtils.isNotBlank(cmd)){
            if ("add".equals(cmd)){
                add(req, resp);
            }
            if ("remove".equals(cmd)){
                remove(req,resp);
            }
            if ("clear".equals(cmd)){
                clear(req,resp);
            }
        }

    }



     synchronized private void add (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //得到商品id
         synchronized ("。"){
        String pidstr = req.getParameter("pid");
        //将id转为整数类型
        Integer pid = new Integer(pidstr);
        //得到context的上下文对象
        ServletContext servletContext = req.getServletContext();
        //得到所有商品列表
        Map<Integer, Product> products = (Map<Integer, Product>) servletContext.getAttribute("products");
        //根据id得到商品
        Product product = products.get(pid);
        //得到连接对象
        HttpSession session = req.getSession();
        //得到购物车对象
        cart cart = (cart) session.getAttribute("cart");
        //如果没有就创建一个（第一次得到时）
        if (cart == null){
            cart = new cart();
        }
        //添加到购物车
        cart.addCart(product);
        //将购物车放到session中
        session.setAttribute("cart",cart);
        //进行页面的转发或重定向
        req.getRequestDispatcher("/index.jsp").forward(req,resp);
         }
    }

    private void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //得到购物车
        HttpSession session = req.getSession();
        cart cart = (cart) session.getAttribute("cart");
        //进行清空
        cart.clearCart();
        //页面的转发或重定向
        req.getRequestDispatcher("/cartlist.jsp").forward(req,resp);
    }

    private void remove(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //得到商品id
        String pidstr = req.getParameter("pid");
        //将id转为整数类型
        Integer pid = new Integer(pidstr);
        //得到购物车
        HttpSession session = req.getSession();
        cart cart = (cart) session.getAttribute("cart");
        //移除
        cart.removeCart(pid);
        //页面转发或重定向
        req.getRequestDispatcher("/cartlist.jsp").forward(req,resp);

    }
}
